package com.shawn.spring.boot.mybatis.run;

import com.github.pagehelper.PageInfo;
import com.shawn.spring.boot.mybatis.constants.MenuTypeEnum;
import com.shawn.spring.boot.mybatis.entity.Menu;
import com.shawn.spring.boot.mybatis.entity.Role;
import com.shawn.spring.boot.mybatis.entity.User;
import com.shawn.spring.boot.mybatis.service.MenuService;
import com.shawn.spring.boot.mybatis.service.RoleService;
import com.shawn.spring.boot.mybatis.service.UserService;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

/**
 * @author shawn
 * @since 2020/8/30
 */
@Component
@Slf4j
public class MybatisRunner implements ApplicationRunner {

  @Autowired private UserService userService;
  @Autowired private RoleService roleService;
  @Autowired private MenuService menuService;

  @Override
  public void run(ApplicationArguments args) throws Exception {
    newMenu();
    queryMenuById();
    newRole();
    queryRoleById();
    queryByLikeName();
    queryMenuByRoleId();
    newUser();
    login();
    queryRolesByUserId();
    queryMenusByPid();
    testPageQuery();
  }

  public void newMenu() {
    Menu menu = new Menu();
    menu.withName("系统管理")
        .withParentId(Long.valueOf(0))
        .withOrderNum(0)
        .withUrl("/")
        .withType(MenuTypeEnum.CATALOG_TYPE.getCode());
    menuService.add(menu);
    log.info("success create new menu");
  }

  public void queryMenuById() {
    Menu menu = menuService.findById(1);
    log.info("queryMenuById: {}", menu.toString());
  }

  public void newRole() {
    Role role = new Role();
    role.withRemark("系统管理员").withRoleName("系统管理员");
    roleService.add(role);
    log.info("success create new role");
  }

  public void queryRoleById() {
    Role role = roleService.findById(1);
    log.info("queryRoleById: {}", role.toString());
  }

  public void queryByLikeName() {
    List<Role> roleList = roleService.findByName("系统");
    if (!CollectionUtils.isEmpty(roleList)) {
      roleList.forEach(role -> log.info("queryByLikeName: {}", role.toString()));
    }
  }

  public void queryMenuByRoleId() {
    List<Menu> menuList = menuService.findByRoleId(1);
    if (!CollectionUtils.isEmpty(menuList)) {
      menuList.forEach(menu -> log.info("queryMenuByRoleId: {}", menu.toString()));
    }
  }

  public void newUser() {
    User user =
        new User()
            .withEmail("1111@qq.com")
            .withMobile("12345678901")
            .withPassword("123456")
            .withSalt("12345")
            .withUsername("admin");
    userService.register(user);
    log.info("success create new user");
  }

  public void login() {
    User shawn = userService.login("admin", "123456");
    log.info("login success: {}", shawn.toString());
  }

  public void queryRolesByUserId() {
    List<Role> roleList = roleService.findByUserId(1);
    if (!CollectionUtils.isEmpty(roleList)) {
      roleList.forEach(role -> log.info("queryRolesByUserId: {}", role.toString()));
    }
  }

  public void queryMenusByPid() {
    List<Menu> menuList = menuService.findByPid(0);
    if (!CollectionUtils.isEmpty(menuList)) {
      menuList.forEach(menu -> log.info("queryMenusByPid: {}", menu.toString()));
    }
  }

  public void testPageQuery() {
    PageInfo<User> withPageHelper = userService.findWithPageHelper(1, 10);
    log.info("withPageHelper: {}", withPageHelper);

    PageInfo<User> withRowBounds = userService.findWithRowBounds(1, 10);
    log.info("withRowBounds: {}", withRowBounds);

    PageInfo<User> withParams = userService.findWithParams(1, 10);
    log.info("withParams: {}", withParams);
  }
}
